package gui.userslist;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;


import user.LocalUser;
import user.User;
import utils.IconManager.IconType;
import utils.IconManager;
import controller.MessageController;
import gui.SendFileAction;
import gui.chatarea.OpenTabListener;

public class UserMouseListener extends MouseAdapter {
	
	private MessageController msgCtrl = null ;
	private OpenTabListener openTab = null ;
	private boolean fileTransferEnable ;
	
	private class SendFileItem extends JMenuItem {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public SendFileItem (final MessageController msgCtrl, final User to) {
			this.setAction(new SendFileAction(getParent(), msgCtrl, to)) ;
			this.setText("Envoyer un fichier") ;
			this.setIcon(new ImageIcon(IconManager.getIcon(IconType.FILE))) ;
		}
		
	}
		
	private class OpenTabItem extends JMenuItem {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public OpenTabItem (final OpenTabListener otl, final Set <User> users) {
			this.setAction(new AbstractAction() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void actionPerformed(ActionEvent ae) {
					otl.openTab(users) ;
				}
			}) ;
			this.setText("Voir la conversation") ;
			this.setIcon(new ImageIcon(IconManager.getIcon(IconType.DIALOG))) ;
		}
		
	}
	
	public UserMouseListener (MessageController msgCtrl, OpenTabListener otl) {
		this.msgCtrl = msgCtrl ;
		this.openTab = otl ;
		this.fileTransferEnable = true ;
	}
	

	public void setFileTransferEnable(boolean fileTransferEnable) {
		this.fileTransferEnable = fileTransferEnable ;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		JList itemList = (JList)e.getSource() ;
		if (e.getButton() == MouseEvent.BUTTON3 && itemList.getSelectedIndices().length > 0) {
			/* Check if mouse event happened on a selected user... */
			int indexClick = itemList.locationToIndex(e.getPoint()) ;
			boolean isSelected = false ;
			for (int index : itemList.getSelectedIndices()) {
				if (indexClick == index) {
					isSelected = true ;
				}
			}
			if (!isSelected) {
				itemList.setSelectedIndex(indexClick) ;
			}
			/* Get users selected, an show menu... */
			int[] selectedIndices = itemList.getSelectedIndices() ;
			Set <User> users = new HashSet <User> () ;
			for (int indice : selectedIndices) {
				users.add((User)itemList.getModel().getElementAt(indice)) ;
			}
			JPopupMenu popup = new JPopupMenu() ;
			popup.add(new OpenTabItem(UserMouseListener.this.openTab, users)) ;
			if (selectedIndices.length == 1 && UserMouseListener.this.fileTransferEnable) {
				User u = (User)itemList.getModel().getElementAt(selectedIndices[0]) ;
				if (!LocalUser.isLocalUser(u)) {
					popup.add(new SendFileItem(UserMouseListener.this.msgCtrl, u)) ;
				}
			}
			popup.show(e.getComponent(),
          		  e.getX(), e.getY());
		}
	}
	
}
